package com.szholly.pro.watergas_server.common.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.szholly.data.general.RowBase;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonUtil {

	static JsonConfig config = new JsonConfig();
	static JsonConfig noFieldIsNewConfig = null;

	static {
		config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() {

			private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

			@Override
			public Object processObjectValue(String key, Object value, JsonConfig cfg) {
				if (value == null) {
					return "";
				}
				return format.format((Date) value);
			}

			@Override
			public Object processArrayValue(Object value, JsonConfig cfg) {
				return format.format((Date) value);
			}
		});
		
		config.registerJsonValueProcessor(Double.class, new JsonValueProcessor() {

			@Override
			public Object processObjectValue(String key, Object value, JsonConfig cfg) {
				if (value == null) {
					return "";
				}
				return value.toString();
			}

			@Override
			public Object processArrayValue(Object value, JsonConfig cfg) {
				if (value == null) {
					return "";
				}
				return value.toString();
			}
		});

		config.registerJsonValueProcessor(Timestamp.class, new JsonValueProcessor() {

			private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			@Override
			public Object processObjectValue(String key, Object value, JsonConfig cfg) {
				if (value == null) {
					return "";
				}
				return format.format((Timestamp) value);
			}

			@Override
			public Object processArrayValue(Object value, JsonConfig cfg) {
				return format.format((Date) value);
			}
		});

		config.registerJsonValueProcessor(oracle.sql.BLOB.class, new JsonValueProcessor() {

			@Override
			public Object processObjectValue(String key, Object value, JsonConfig cfg) {
				if (value == null) {
					return "";
				}
				String reString = "";
				oracle.sql.BLOB clob = (oracle.sql.BLOB) value;
				try {

					InputStreamReader is = new InputStreamReader(clob.getStream());// 得到流
					BufferedReader br = new BufferedReader(is);
					String s = br.readLine();
					StringBuffer sb = new StringBuffer();
					while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
						sb.append(s);
						s = br.readLine();
						reString = sb.toString();
					}
					return reString;
				} catch (Exception e) {
					return "json convert error";
				}
			}

			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}

		});

		config.registerJsonValueProcessor(oracle.sql.CLOB.class, new JsonValueProcessor() {

			@Override
			public Object processObjectValue(String key, Object value, JsonConfig cfg) {
				if (value == null) {
					return "";
				}
				String reString = "";
				oracle.sql.CLOB clob = (oracle.sql.CLOB) value;
				try {
					Reader is = clob.getCharacterStream();// 得到流
					BufferedReader br = new BufferedReader(is);
					String s = br.readLine();
					StringBuffer sb = new StringBuffer();
					while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
						sb.append(s);
						s = br.readLine();
						reString = sb.toString();
					}
					return reString;
				} catch (Exception e) {
					return "json convert error";
				}
			}

			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}

		});
		config.registerJsonValueProcessor(oracle.sql.STRUCT.class, new JsonValueProcessor() {

			@Override
			public Object processObjectValue(String key, Object value, JsonConfig cfg) {
				if (value == null) {
					return "";
				}
				String reString = "";
				return reString;
			}

			@Override
			public Object processArrayValue(Object arg0, JsonConfig arg1) {
				return null;
			}

		});
		
		noFieldIsNewConfig = config.copy();
		noFieldIsNewConfig.setExcludes(new String[]{RowBase.FIELD_ISNEW});
	}

	public static JsonConfig getConfig() {
		return config;
	}
	
	public static JsonConfig getNoFieldIsNewConfig() {
		return noFieldIsNewConfig;
	}

	public static String ConvertArrayToJson(Object obj) {
		if (obj == null) {
			return "";
		}
		JSONArray jsonObj = JSONArray.fromObject(obj, config);
		String jsonStr = jsonObj.toString();
		return jsonStr;
	}

	public static String ConvertObjectToJson(Object obj) {
		if (obj == null) {
			return "";
		}
		JSONObject jsonObj = JSONObject.fromObject(obj, config);
		String jsonStr = jsonObj.toString();
		return jsonStr;
	}
	public static JSONArray ConvertArrayToJsonObject(Object obj) {
		if (obj == null) {
			return null;
		}
		JSONArray jsonObj = JSONArray.fromObject(obj, config);
		return jsonObj;
	}
	public static JSONObject ConvertObjectToJsonObject(Object obj) {
		if (obj == null) {
			return null;
		}
		JSONObject jsonObj = JSONObject.fromObject(obj, config);
		return jsonObj;
	}
}